<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="报事人" prop="reporter">-->
        <!--<el-input-->
          <!--v-model="queryParams.reporter"-->
          <!--placeholder="请输入报事人"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="联系电话" prop="phone">-->
        <!--<el-input-->
          <!--v-model="queryParams.phone"-->
          <!--placeholder="请输入联系电话"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="工单来源" prop="orderSource">
        <el-select v-model="queryParams.orderSource" placeholder="请选择工单来源" clearable size="small">
          <el-option
            v-for="dict in orderSourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="工单状态" prop="state">-->
<!--        <el-select v-model="queryParams.state" placeholder="请选择工单状态">-->
<!--          <el-option-->
<!--            v-for="dict in repairStateOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <!--<el-form-item label="处理人" prop="handlerId">-->
        <!--<el-select clearable v-model="queryParams.handlerId" placeholder="请选择处理人">-->
          <!--<el-option v-for="item in wyUserList" :key="item.id" :label="item.userName" :value="item.id">-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="责任分担" prop="dutySharing">
        <el-select v-model="queryParams.dutySharing" placeholder="请选择责任分担" clearable size="small">
          <el-option
            v-for="dict in dutySharingOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修类型" prop="repairType">
        <el-select v-model="queryParams.repairType" placeholder="请选择维修类型" clearable size="small">
          <el-option
            v-for="dict in repairTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['park:wyRepairManagement:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:wyRepairManagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:wyRepairManagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wyRepairManagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="Id" align="center" prop="id" />-->
      <el-table-column label="报事人" align="center" prop="reporter" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="工单来源" align="center" prop="orderSource" :formatter="orderSourceFormat" />
      <el-table-column label="反应问题" align="center" prop="problem" />
      <el-table-column label="工单状态" align="center" prop="state">
        <template slot-scope="scope">
          <span :style="{color:wxtype[scope.row.state].color}">
                      {{wxtype[scope.row.state].state}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任分担" align="center" prop="dutySharing" :formatter="dutySharingFormat" />
      <!--<el-table-column label="处理人" align="center" prop="result" />-->
      <!--<el-table-column label="处理人电话" align="center" prop="handler" width="106px"/>-->
      <!--<el-table-column label="处理结果" align="center" prop="handlerPhone" />-->
      <!--<el-table-column label="处理耗时" align="center" prop="duration">-->
        <!--<template slot-scope="scope">-->
          <!--<span v-if="scope.row.state==4">-->
            <!--{{dataMix(scope.row.createTime,scope.row.updateTime)}}-->
          <!--</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="维修费用" align="center" prop="repairFee" />
      <el-table-column label="维修类型" align="center" prop="repairType"/>
<!--      <el-table-column label="维修类型" align="center" prop="repairType" :formatter="repairTypeFormat" />-->
      <el-table-column label="报修地点" align="center" prop="repairAddress" />
      <!--<el-table-column label="结束原因" align="center" prop="endReason" />-->
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            @click="okPay(scope.row)"
            v-if="scope.row.state==1"
            v-hasPermi="['park:wyRepairManagement:edit']"
          >确认缴费</el-button>

          <el-button
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-if="wxtype[scope.row.state].btnTxt"
            v-hasPermi="['park:wyRepairManagement:edit']"
          >{{ wxtype[scope.row.state].btnTxt }}</el-button>


          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:wyRepairManagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报事人" prop="reporter">
          <el-input v-model="form.reporter" placeholder="请输入报事人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="工单来源" prop="orderSource">
          <el-select v-model="form.orderSource" placeholder="请选择工单来源">
            <el-option
              v-for="dict in orderSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修类型" prop="repairType">
          <el-select v-model="form.repairType" placeholder="请选择维修类型">
            <el-option
              v-for="dict in repairTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报修地点" prop="repairAddress">
          <el-input v-model="form.repairAddress" placeholder="请输入报修地点" />
        </el-form-item>
        <el-form-item label="结束原因" prop="endReason" v-if="form.state==1">
          <el-input v-model="form.endReason" placeholder="请输入结束原因" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    派工 选择处理人s-->
    <el-dialog :title="editTitle" :visible.sync="showPaigong" width="500px" append-to-body>
      <el-form label-width="80px">

        <el-form-item label="维修费用" prop="repairFee" v-if="editForm.state==0">
          <el-input type="number" v-model="editForm.repairFee" placeholder="请输入维修费用" />
        </el-form-item>

        <el-form-item label="结束原因" prop="repairFee" v-if="editForm.state==1" >
          <el-input v-model="editForm.endReason" placeholder="请输入结束原因" />
        </el-form-item>

        <el-form-item label="人员类型" v-if="editForm.state==0">
          <el-select @change="selectUser" clearable v-model="editForm.userType" placeholder="请选择人员类型" >
            <el-option  v-for="item in propertyUserType" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理人" v-if="editForm.state==0">
          <el-select clearable v-model="editForm.handlerId" placeholder="请选择处理人" >
            <el-option v-for="item in wyUserList" :key="item.id" :label="item.userName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="责任分担"  v-if="editForm.state==0">
          <el-select v-model="editForm.dutySharing" placeholder="请选择责任分担">
            <el-option
              v-for="dict in dutySharingOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okPaigong">确 定</el-button>
        <el-button @click="cancelFn">取 消</el-button>
      </div>
    </el-dialog>
<!--    派工 选择处理人e-->
  </div>
</template>

<script>
import { listWyRepairManagement, getWyRepairManagement, delWyRepairManagement, addWyRepairManagement, updateWyRepairManagement } from "@/api/smartParks/property/repair.js";
import { listWyUser } from "@/api/smartParks/property/propertyGreeningPeople";
import { GetDateDiff } from '@/utils/index'
import { listData } from "@/api/system/dict/data"; //字典接口
export default {
  name: "repairPay",
  components: {
  },
  data() {
    return {
      wxtype: [
        {state:'待处理',color:"#F74D39",btnColor:'#44D94B',txt:"请选择维修人员进行派工",btnTxt:"派工"},
        {state:'未缴费',color:"#FF5E00",btnColor:'#F74D39',txt:"报事人尚未缴费，请等待…",btnTxt:"结束"},
        {state:'已缴费',color:"#FF5E00",btnColor:'#44D94B',txt:"已缴费100元，请尽快进行维修",btnTxt:"开始维修"},
        {state:'维修中',color:"#FF5E00",btnColor:'#44D94B',txt:"维修人员正在维修，请等待反馈",btnTxt:"完成维修"},
        {state:'受理完成',color:"#44D94B",btnColor:'',txt:"工单已完成",btnTxt:""},
        {state:'结束受理',color:"#F74D39",btnColor:'',txt:"未缴费",btnTxt:""}],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      editTitle:'',
      // 总条数
      total: 0,
      // 维修管理表格数据
      wyRepairManagementList: [],
      // 弹出层标题
      title: "",
      wyUserList:[],
      // 是否显示弹出层
      open: false,
      // 工单来源字典
      orderSourceOptions: [],
      // 责任分担字典
      dutySharingOptions: [],
      // 维修类型字典
      repairTypeOptions: [],
      //修改表单
      editForm:[],
      //是否显示选择派工
      showPaigong:false,
      //是否结束
      showJieShu:false,
      dateRange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reporter: null,
        phone: null,
        orderSource: null,
        problem: null,
        state: null,
        handlerId: null,
        dutySharing: null,
        result: null,
        duration: null,
        repairFee: null,
        repairType: null,
        repairAddress: null,
        endReason: null,
        stateList:[1]
      },
      repairStateOptions:[],
      // 表单参数
      form: {},
      loadingOption:{
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      },
      // 表单校验
      rules: {
      },
      propertyUserType:[]
    };
  },
  created() {
    this.getList();
    listData({ dictType: "property_user_type" }).then((response) => {
      this.propertyUserType = response.rows;
    });
    this.getDicts("repair_state").then(response => {
      this.repairStateOptions = response.data;
    });
    this.getDicts("order_source").then(response => {
      this.orderSourceOptions = response.data;
    });
    this.getDicts("duty_sharing").then(response => {
      this.dutySharingOptions = response.data;
    });
    this.getDicts("repair_type").then(response => {
      this.repairTypeOptions = response.data;
    });
  },
  methods: {
    // 筛选人员
    selectUser(val){
      console.log(val);
        listWyUser({'userType':val}).then((response) => {
          this.wyUserList = response.rows;
        });
    },
    /** 查询维修管理列表 */
    getList() {
      this.loading = true;
      listWyRepairManagement(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.wyRepairManagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    gLoad(){
      return this.$loading(this.loadingOption);
    },
    //计算时差
    dataMix(time1,time2){
      return GetDateDiff(time1,time2,"minute")
    },
    // 工单来源字典翻译
    orderSourceFormat(row, column) {
      return this.selectDictLabel(this.orderSourceOptions, row.orderSource);
    },
    // 责任分担字典翻译
    dutySharingFormat(row, column) {
      return this.selectDictLabel(this.dutySharingOptions, row.dutySharing);
    },
    // 维修类型字典翻译
    repairTypeFormat(row, column) {
      return this.selectDictLabel(this.repairTypeOptions, row.repairType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelFn(){
      this.showPaigong=false
      this.editReset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reporter: null,
        phone: null,
        orderSource: null,
        problem: null,
        state: null,
        handlerId: null,
        dutySharing: null,
        result: null,
        duration: null,
        createBy: null,
        repairFee: null,
        createTime: null,
        repairType: null,
        repairAddress: null,
        updateBy: null,
        endReason: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    editReset(){
      this.editForm = {
        id: null,
        reporter: null,
        phone: null,
        orderSource: null,
        problem: null,
        state: null,
        handlerId: null,
        userType:null,
        dutySharing: null,
        result: null,
        duration: null,
        createBy: null,
        repairFee: null,
        createTime: null,
        repairType: null,
        repairAddress: null,
        updateBy: null,
        endReason: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    openFullScreen2() {

  },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWyRepairManagement(id).then(response => {
        this.editForm = response.data;
        if (this.editForm.state>1){
          this.$confirm('是否确认操作?', "确认操作?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(_=> {
            const load=this.gLoad()
            this.editForm.state=parseInt(this.editForm.state)+1
            updateWyRepairManagement( this.editForm).then(response => {
              load.close()
              this.msgSuccess("操作成功");
              this.getList();
              this.cancelFn()
            }).catch(err=>{
              data.state=0
            })
          })
        }else {
          this.showPaigong=true
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWyRepairManagement(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWyRepairManagement(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除维修管理编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delWyRepairManagement(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('park/wyRepairManagement/export', {
        ...this.queryParams
      }, `park_wyRepairManagement.xlsx`)
    },
    // 确认支付
    okPay(data){
      this.$confirm('是否确认该单已支付?', "是否支付", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(_=> {
        data.state=2
        const load=this.gLoad()
        updateWyRepairManagement(data).then(response => {
          load.close()
          this.msgSuccess("缴费成功");
          this.getList();
        }).catch(err=>{
          data.state=1
        })
      })
    },
    //确认派工
    okPaigong(data){
      this.$confirm('是否确认操作?', "确认操作?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(_=> {
        const load=this.gLoad()
        if (this.editForm.state==1){
          this.editForm.state=5
        }else {
          this.editForm.state=parseInt(this.editForm.state)+1
        }
        updateWyRepairManagement( this.editForm).then(response => {
          load.close()
          this.msgSuccess("操作成功");
          this.getList();
          this.cancelFn()
        }).catch(err=>{
          data.state=0
        })
      })
    }
  }
};
</script>
